package com.itheima.chapter03.repository;

import com.itheima.chapter03.entity.Discuss;
import jakarta.transaction.Transactional;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface DiscussRepository extends JpaRepository<Discuss, Integer> {
    List<Discuss> findByAuthorNotNull();
    List<Discuss> findByAuthorIsNull();
    List<Discuss> findByContentNotNull();

    @Query("SELECT c FROM t_comment c WHERE c.aId = ?1")
    List<Discuss> getDiscussPaged(Integer aid, Pageable pageable);

    @Query(value = "SELECT * FROM t_comment WHERE a_id = ?1", nativeQuery = true)
    List<Discuss> getDiscussPaged2(Integer aid, Pageable pageable);

    @Transactional
    @Modifying
    @Query("UPDATE t_comment c SET c.author = ?1 WHERE c.id = ?2")
    int updateDiscuss(String author, Integer id);

    @Transactional
    @Modifying
    @Query(value = "INSERT t_comment (author, content, a_id) VALUES (?1, ?2, ?3", nativeQuery = true)
    int insertDiscuss(String author, String content, Integer aId);

    @Transactional
    @Modifying
    @Query("DELETE t_comment c WHERE c.id = ?1")
    int deleteDiscuss(Integer id);
}
